package com.meichis.mcsappframework.encrypt;

import android.text.TextUtils;
import android.util.Base64;
import android.util.Log;

import com.meichis.mcsappframework.BuildConfig;
import com.meichis.mcsappframework.utils.GzipTool;
import com.meichis.mcsappframework.utils.SharePreferenceUtil;

import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;

public class AESProvider {
    public static final String TAG = "AESUtils";

    private static String CryptAESKey = "";
    private static String CryptAESIV = "";

    public static void init(String cryptAESKey, String cryptAESIV) {
        CryptAESKey = cryptAESKey;
        CryptAESIV = cryptAESIV;
        SharePreferenceUtil.getInstance().setStringValue("AY", cryptAESKey);
        SharePreferenceUtil.getInstance().setStringValue("AV", cryptAESIV);
    }

    /*
     * 加密
     */
    public static String encrypt(String clearText) {
        if (BuildConfig.DEBUG) {
            Log.d(TAG, "加密前的内容为:" + clearText);
        }
        if (TextUtils.isEmpty(CryptAESKey)) {
            CryptAESKey = SharePreferenceUtil.getInstance().getStringValue("AY");
            CryptAESIV = SharePreferenceUtil.getInstance().getStringValue("AV");
        }
        byte[] result = null;
        try {
            byte[] rawkey = CryptAESKey.getBytes("utf-8");
            byte[] biv = CryptAESIV.getBytes("utf-8");
            result = encrypt(rawkey, biv, clearText.getBytes());
        } catch (Exception e) {
            e.printStackTrace();
        }
        String content = "";
        try {
            content = Base64.encodeToString(result, Base64.DEFAULT);
        } catch (Exception e) {
            // TODO: handle exception
        }

        if (BuildConfig.DEBUG) {
            Log.d(TAG, "加密后的内容为:" + content);
        }
        return content;

    }

    public static String decrypt(String encrypted) {
        if (TextUtils.isEmpty(CryptAESKey)) {
            CryptAESKey = SharePreferenceUtil.getInstance().getStringValue("AY");
            CryptAESIV = SharePreferenceUtil.getInstance().getStringValue("AV");
        }
        if (TextUtils.isEmpty(encrypted)) {
            return "";
        }
        if (BuildConfig.DEBUG) {
            Log.d(TAG, "解密前的内容为:" + encrypted);
        }
        byte[] rawKey;
        try {
            rawKey = CryptAESKey.getBytes("utf-8");
            byte[] enc = Base64.decode(encrypted, Base64.DEFAULT);
            byte[] biv = CryptAESIV.getBytes("utf-8");
            byte[] result = decrypt(rawKey, biv, enc);
            String coentn = new String(result);
            if (coentn.startsWith("H4sI")) {
                coentn = GzipTool.decryptGZIP(coentn);
            }
            if (BuildConfig.DEBUG) {
                Log.d(TAG, "解密后的内容为:" + coentn);
            }
            return coentn;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }

    }

    private static byte[] encrypt(byte[] raw, byte[] biv, byte[] clear) throws Exception {
        SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
        Cipher cipher = Cipher.getInstance("AES/CBC/PKCS7Padding");
        IvParameterSpec iv = new IvParameterSpec(biv);
        cipher.init(Cipher.ENCRYPT_MODE, skeySpec, iv);
        byte[] encrypted = cipher.doFinal(clear);
        return encrypted;
    }

    private static byte[] decrypt(byte[] raw, byte[] biv, byte[] encrypted) throws Exception {
        SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
        Cipher cipher = Cipher.getInstance("AES/CBC/PKCS7Padding");
        IvParameterSpec iv = new IvParameterSpec(biv);
        cipher.init(Cipher.DECRYPT_MODE, skeySpec, iv);
        byte[] decrypted = cipher.doFinal(encrypted);
        return decrypted;
    }

}
